<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>add Product</title>
</head>
<script type='text/javascript' src='js/prototype.js'></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type='text/javascript'>
	function applyTagsToProduct(productId){
		var tags=document.getElementById("product_tag").value;
		if(null==tags||""==tags)
			return null;
		var tagArray=tags.split(",");
		var params="productId="+productId;
		for(var i=0;null!=tagArray[i]&&""!=tagArray[i];i++)
			params+="&product_tagName="+tagArray[i];
		new Ajax.Request("json/applyTagsToProduct", {
			method : "post",
			parameters : params,
			onComplete : function(request) {
				var res = request.responseText.evalJSON();
				if (res["result"]["flag"] == "true") {
					window.location.href = "./productOwner.html";
				} else
					alert("错误！");
			},
			asynchronous : false
		});
	}
	function addProduct() {
		var name = document.getElementById("name").value;
		var price = document.getElementById("price").value;
		var amount = document.getElementById("amount").value;
		var content = CKEDITOR.instances.editor01.getData();
		var params = "product_keyName=name&product_keyName=price&product_keyName=amount&product_keyValue="
				+ encodeURIComponent(name)
				+ "&product_keyValue="
				+ encodeURIComponent(price)
				+ "&product_keyValue="
				+ encodeURIComponent(amount)
				+ "&productDescription="
				+ encodeURIComponent(content);
		new Ajax.Request("json/addProduct", {
			method : "post",
			parameters : params,
			onComplete : function(request) {
				var res = request.responseText.evalJSON();
				if (res["result"]["flag"] == "true") {
					applyTagsToProduct(res["result"]["productId"]);
					window.location.href = "./productOwner.html";
				} else
					alert("错误！");
			},
			asynchronous : false
		});
	}
</script>
<body>
	<div class="topbar">
		<ul class="horizon">
			<li style="margin-left: 50%; margin-top: 2px;" class="logo"><a
				href="login.html">首页</a></li>
			<li class="login" id="login">
				<ul class="vertical" style="margin-left: 80px">
					<li class="userInfo"><img src="image/userPhoto.png" /><a>&nbsp;你好，用户</a></li>
					<li class="userInfo2">
						<ul class="horizon">
							<li style="margin-right: 15px;"><img
								src="image/userPhotoBig.png" /></li>
							<li>用户&nbsp;&nbsp;&nbsp;&nbsp;<a>修改密码</a><br />编辑个人资料<br />注销<br /></li>
						</ul>
					</li>
				</ul>
			</li>
		</ul>
	</div>
	<div class="main">
		<ul class="horizon">
			<li>
				<ul class="vertical">
					<li class="managePad">
						<ul class='vertical'>
							<li>
								<!-- <a href="./addProduct.html">add product</a>  -->
							</li>
						</ul>
					</li>
				</ul>
			<li class="content">
				<ul class="horizon">
					<li>名字<input type="text" id="name" /></li>
					<li>价格<input type="text" id="price" /></li>
					<li>数量<input type="text" id="amount" /></li>
					<li>检索标签（请用英文逗号","隔开）<input type="text" id="product_tag" /></li>
					<li><textarea rows="30" cols="50" name="editor01">请输入详尽介绍.</textarea>
						<script type="text/javascript">
							CKEDITOR.replace('editor01');
						</script>
					</li>
					<li><input type="button" value="add" onclick="addProduct()" /></li>
				</ul>
			</li>
		</ul>
	</div>
</body>
</html>